/**
 * 
 */
package com.jushu.storbox.service;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;

import com.jushu.storbox.database.DAOFactory;
import com.jushu.storbox.entities.FileFolderInfo;
import com.jushu.storbox.entities.FolderInfo;
import com.jushu.storbox.util.LogUtil;
import com.huawei.sharedrive.sdk.android.exception.ClientException;
import com.huawei.sharedrive.sdk.android.modelV2.response.INodeShareV2;

public class RemoteINodeService extends AbstractBaseService implements
		IRemoteINodeService {
	private final static String TAG = "RemoteINodeService";
	private boolean isChange;
	@Override
	public List<FileFolderInfo> getInodeFileFolderList(String folderId,
			String ownerId, Context context, String desc, int limit,
			int offset, String orderby) throws ClientException {
		// TODO Auto-generated method stub
		boolean isChanged = false;
		List<FileFolderInfo> folderInfoList = null;
		long nowTime = System.currentTimeMillis();
		List<INodeShareV2> response = null;
		folderInfoList = DAOFactory.instance(context).getShareINodeDao()
				.getFileList(folderId, ownerId);
        shareDriveApplication.smartAuthorization();
		response = FileHelper.getSharedList(desc, limit, offset, orderby);
		isChanged = FileHelper.compareServiceClientINode(context, response, folderInfoList);
		setInodeIsChange(isChanged);
		if ("0".equals(folderId)) {
			FolderInfo nodeShare = new FolderInfo();
			nodeShare.setiNodeId(folderId);
			nodeShare.setOwnerId(ownerId);
			nodeShare.setLastRefreshTime(nowTime);
			DAOFactory
					.instance(context)
					.getShareINodeDao()
					.insertOrUpdateShareFolder(context, folderId, ownerId,
							nodeShare);
		}
		folderInfoList.clear();
		folderInfoList = getInodeFileFolderListDB(folderId, ownerId, context, "desc", limit, offset, "modified_at");

		return folderInfoList;
	}
	@Override
	public void setInodeIsChange(boolean isShareChanged) {
		// TODO Auto-generated method stub
		this.isChange = false;
		LogUtil.i(TAG, "folderisChange:[setInodeIsChange]"+isShareChanged);
		this.isChange = isShareChanged;
	}
	
	@Override
	public List<FileFolderInfo> getInodeFileFolderListDB(String folderId,
			String ownerId, Context context, String desc, int limit,
			int offset, String orderby) throws ClientException {
		// TODO Auto-generated method stub
		List<FileFolderInfo> filefolderInfoList = new ArrayList<FileFolderInfo>();
//		folderInfoList = DAOFactory.instance(context).getShareINodeDao()
//				.getFileList(folderId, ownerId);
		List<FileFolderInfo> folderList = DAOFactory.instance(context).getShareINodeDao().getFolderList(folderId, ownerId, desc, orderby);
		List<FileFolderInfo> fileList = DAOFactory.instance(context).getShareINodeDao().getFileList(folderId, ownerId, desc, orderby);
		filefolderInfoList.addAll(folderList);
		filefolderInfoList.addAll(fileList);
		return filefolderInfoList;
	}
	@Override
	public boolean getInodeIsChange() {
		// TODO Auto-generated method stub
		return this.isChange;
	}
}
